﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using XT.BusinessService;
using XT.Model;
using XT.Web.External;


namespace XT.Web.Controllers
{
    public class AreaController : Controller
    {
        //
        // GET: /Area/

        public ActionResult Index(int id)
        {
            var areaService = MvcApplication.GetInstance<IAreaService>();
            Area area = areaService.GetAreaByID(id);
            return View(area);
        }

        public ActionResult ShowRooms(int areaId)
        {
            var roomService = MvcApplication.GetInstance<IRoomService>();
            IEnumerable<Room> lstRoom = roomService.getSomeRoomByArea(areaId,1,3);
            return PartialView("../Global/Rooms",lstRoom);
        }

        public ActionResult ShowMoreRooms(int areaId,int pageIndex)
        {
            var roomService = MvcApplication.GetInstance<IRoomService>();
            IEnumerable<Room> lstRoom = roomService.getSomeRoomByArea(areaId, pageIndex, 3);
            return Json(new { content = Helper.RenderViewToString(this.ControllerContext, "../Global/Rooms", lstRoom) });
        }

        public ActionResult GetLocation()
        {
            var areaService = MvcApplication.GetInstance<IAreaService>();
            Area area = areaService.GetAreaByID(1);
            XElement xmlLocation = area.Area_Location;
            List<XElement> location = (from l in xmlLocation.Elements("point")
                                       select l).ToList();
            List<Models.Point> points = new List<Models.Point>();
            foreach (XElement point in location)
            {
                Models.Point _point = new Models.Point();
                _point.latitude = double.Parse(point.Element("latitude").Value, System.Globalization.CultureInfo.InvariantCulture);
                _point.longitude = double.Parse(point.Element("longitude").Value, System.Globalization.CultureInfo.InvariantCulture);
                points.Add(_point);
            }
            return Json(points);
        }
    }
}
